import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    // 四个tab标签页
    {
        path: '/',
        name: 'Index',
        component: () =>
            import ('@/views/Index.vue'),
        meta: { title: '首页' }
    },
    {
        path: '/menu',
        name: 'Menu',
        component: () =>
            import ('@/views/Menu.vue'),
        meta: { title: '分类' }
    },
    {
        path: '/car',
        name: 'Car',
        component: () =>
            import ('@/views/Car.vue'),
        meta: { title: '购物车' }
    },
    {
        path: '/user',
        name: 'User',
        component: () =>
            import ('@/views/User.vue'),
        meta: { title: '我的' }
    },
    // 品牌街
    {
        path: '/brand',
        name: 'Brand',
        component: () =>
            import ('@/views/Brand.vue'),
        meta: { title: '品牌街' }
    },
    // 店铺路由
    {
        path: '/store/index',
        name: 'StoreIndex',
        component: () =>
            import ('@/views/Store/Index.vue'),
        meta: { title: '店铺街' }
    },
    {
        path: '/store/store/:id',
        name: 'StoreStore',
        component: () =>
            import ('@/views/Store/Store.vue'),
        meta: { title: '店铺信息(商品)' }
    },
    {
        path: '/store/info/:id',
        name: 'StoreStore',
        component: () =>
            import ('@/views/Store/Info.vue'),
        meta: { title: '店铺简介(文字)' }
    },
    // 用户模块
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('@/views/User/Login.vue'),
        meta: { title: '登录' }
    },
    {
        path: '/reg',
        name: 'Reg',
        component: () =>
            import ('@/views/User/Reg.vue'),
        meta: { title: '注册' }
    },
    {
        path: '/user/order',
        name: 'UserOrder',
        component: () =>
            import ('@/views/User/Order.vue'),
        meta: { title: '我的订单', needLogin: true }
    },
    {
        path: '/user/set',
        name: 'UserSet',
        component: () =>
            import ('@/views/User/Setting.vue'),
        meta: { title: '我的设置', needLogin: true }
    },
    // 商品模块
    {
        path: '/goodlist/:id',
        name: 'GoodList',
        component: () =>
            import ('@/views/Goods/List.vue'),
        meta: { title: '商品列表' }
    },
    {
        path: '/goodinfo/:id',
        name: 'GoodInfo',
        component: () =>
            import ('@/views/Goods/Info.vue'),
        meta: { title: '商品详情' }
    },
    // 404
    {
        path: '/404',
        name: 'NotFound',
        component: () =>
            import ('@/views/NotFound.vue'),
        meta: { title: '页面走丢了' }
    },
    {
        path: '/*',
        redirect: '/404'
    }
]

const router = new VueRouter({
    routes
})

// 导航守卫了
router.beforeEach((to, from, next) => {
    document.title = to.meta.title
        // 去的页面是否需要登录
    if (to.meta.needLogin) {
        // 需要登录
        if (sessionStorage.getItem('userinfo')) {
            next()
        } else {
            next('/login')
        }
    } else {
        // 不需要登录
        next()
    }
})

export default router